﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace Utility.Xml
{
	// Token: 0x0200065C RID: 1628
	public class Control
	{
		// Token: 0x060026AA RID: 9898 RVA: 0x00148614 File Offset: 0x00146814
		public Control(string savePath, string saveName, string rootName, List<Data> dataList)
		{
			this.savePath = savePath;
			this.saveName = saveName;
			this.rootName = rootName;
			this.dataList = dataList;
			this.Init();
		}

		// Token: 0x17000416 RID: 1046
		// (get) Token: 0x060026AB RID: 9899 RVA: 0x0014864C File Offset: 0x0014684C
		public List<Data> DataList
		{
			get
			{
				return this.dataList;
			}
		}

		// Token: 0x17000417 RID: 1047
		public Data this[int index]
		{
			get
			{
				return this.dataList[index];
			}
		}

		// Token: 0x060026AD RID: 9901 RVA: 0x00148664 File Offset: 0x00146864
		public virtual void Init()
		{
			foreach (Data data in this.dataList)
			{
				data.Init();
			}
		}

		// Token: 0x060026AE RID: 9902 RVA: 0x001486C0 File Offset: 0x001468C0
		public virtual void Write()
		{
			string file = global::UserData.Create(this.savePath) + this.saveName;
			XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
			xmlWriterSettings.Indent = true;
			xmlWriterSettings.Encoding = Encoding.UTF8;
			XmlWriter xmlWriter = null;
			try
			{
				xmlWriter = XmlWriter.Create(file, xmlWriterSettings);
				xmlWriter.WriteStartDocument();
				xmlWriter.WriteStartElement(this.rootName);
				foreach (Data data in this.dataList)
				{
					data.Write(xmlWriter);
				}
				xmlWriter.WriteEndElement();
				xmlWriter.WriteEndDocument();
			}
			finally
			{
				if (xmlWriter != null)
				{
					xmlWriter.Close();
				}
			}
		}

		// Token: 0x060026AF RID: 9903 RVA: 0x00148794 File Offset: 0x00146994
		public virtual void Read()
		{
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				string text = string.Concat(new object[]
				{
					global::UserData.Path,
					this.savePath,
					'/',
					this.saveName
				});
				if (File.Exists(text))
				{
					xmlDocument.Load(text);
					foreach (Data data in this.dataList)
					{
						data.Read(this.rootName, xmlDocument);
					}
				}
			}
			catch (Exception ex)
			{
				global::Debug.LogError(ex.Message);
			}
		}

		// Token: 0x04002DC9 RID: 11721
		protected readonly string savePath;

		// Token: 0x04002DCA RID: 11722
		protected readonly string saveName;

		// Token: 0x04002DCB RID: 11723
		protected readonly string rootName;

		// Token: 0x04002DCC RID: 11724
		protected List<Data> dataList = new List<Data>();
	}
}
